﻿using System.IO;
using System.Threading.Tasks;
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.AspNetCore.Http;
using System.Configuration;
using System.Linq;
using Cloud.Core.Response;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Hosting;


namespace Cloud.Core.Controllers.WebChat
{
    [Route("api/wx/[controller]")]
    [ApiController]
    public class FileUploadController : ControllerBase
    {
        private readonly IWebHostEnvironment _hostEnvironment;

        /// <summary>
        /// 注入服务
        /// </summary>
        /// <param name="hostEnvironment"></param>
        public FileUploadController ( IWebHostEnvironment hostEnvironment )
        {
            _hostEnvironment = hostEnvironment;
        }

        [HttpPost]
        [Route("UploadFile")]
        public ActionResult<JsonResponse> UploadFile ( IFormFile file )
        {

            if (file != null)
            {
                try
                {
                    // 文件后缀
                    var fileExtension = Path.GetExtension(file.FileName);
                    // 判断后缀是否是图片
                    const string fileFilt = ".gif|.jpg|.jpeg|.png";
                    if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1)
                    {
                        //myds.Message = "请上传jpg、png、gif格式的图片";
                    }
                    var strDateTime = DateTime.Now.ToString("yyMMddhhmmssfff"); //取得时间字符串
                    var strRan = Convert.ToString(new Random().Next(100, 999)); //生成三位随机数
                    var saveName = strDateTime + strRan + fileExtension;// 
                    //保存文件
                    string uploadPath = Path.Combine(_hostEnvironment.WebRootPath, "");
                    //var uploadPath = rootPath.Substring(0, rootPath.LastIndexOf('\\'));
                    string dir = "\\upload\\File\\" + DateTime.Now.ToString("yyyy-MM-dd");
                    string filePath = uploadPath + dir + "\\" + saveName;

                    if (!Directory.Exists(uploadPath + dir))
                        Directory.CreateDirectory(uploadPath + dir);
                    // 保存文件到服务器
                    using (FileStream fs = System.IO.File.Create(filePath))
                    {
                        file.CopyTo(fs);
                        fs.Flush();
                    }


                    return new JsonResponse<string>
                    {
                        Data = dir + "\\" + saveName,
                        Remark = saveName,
                        Msg = "上传成功",
                        Status = ResponseStatus.Success
                    };
                }
                catch (Exception ex)
                {
                    return new JsonResponse
                    {
                        Msg = ex.Message,
                        Status = ResponseStatus.ParameterError
                    }; ;
                }
            }
            else
            {

                return new JsonResponse
                {

                    Msg = "参数错误",
                    Status = ResponseStatus.ParameterError
                }; ;
            }

        }

    }


}
